<template>
  <j-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="预算科目编号">
          <a-input placeholder="请输入预算科目编号" v-decorator="['subjectsCode', validatorRules.subjectsCode]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="财务科目编码">
          <a-input placeholder="请输入财务科目编码" v-decorator="['erpCode', validatorRules.erpCode]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="预算科目名称">
          <a-input placeholder="请输入预算科目名称" v-decorator="['subjectsName', validatorRules.subjectsName]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="预算科目类型">
          <cn-dict-select-tag
            placeholder="请输入预算科目类型"
            dict-code="subjects_type"
            triggerChange
            :options="{value:'dictDataCode',label:'dictDataDesc'}"
            v-decorator.trim="[ 'subjectsType', validatorRules.subjectsType]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="预算科目分组">
          <cn-dict-select-tag
            placeholder="请输入预算科目分组"
            dict-code="subjects_group"
            triggerChange
            :options="{value:'dictDataCode',label:'dictDataDesc'}"
            v-decorator.trim="[ 'subjectsGroup', validatorRules.subjectsGroup]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="是否滚动">
          <a-radio-group v-decorator="['isRoll',{}]" placeholder="是否滚动">
            <a-radio value="1">
              是
            </a-radio>
            <a-radio value="0">
              否
            </a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="余额控制">
          <a-radio-group v-decorator="['isControl',{}]" placeholder="是否滚动">
            <a-radio value="1">
              是
            </a-radio>
            <a-radio value="0">
              否
            </a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="备注">
          <a-textarea placeholder="请输入备注" v-decorator="['remarks', {}]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"

  export default {
    name: "TpmBudgetSubjectsModal",
    data () {
      return {
        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
          subjectsCode: {
            rules: [{
              required: true, message: '请输入预算科目编号!'
            }]
          },
          subjectsName: {
            rules: [{
              required: true, message: '请输入预算科目名称!'
            }]
          },
          erpCode: {
            rules: [{
              required: true, message: '请输入财务科目编码!'
            }]
          },
          subjectsType: {
            rules: [{
              required: true, message: '请输入预算科目类型!'
            }]
          },
          subjectsGroup: {
            rules: [{
              required: true, message: '请输入预算科目分组!'
            }]
          },

        },
        url: {
          add: window._CONFIG['domianTpmURL']+"/budget/tpmBudgetSubjects/saveOrUpdate",
        },
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.model.isRoll = this.model.isRoll+"";
        this.model.isControl = this.model.isControl+"";
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'remarks','subjectsCode','erpCode',
            'subjectsName','subjectsType','subjectsGroup','isRoll','isControl'))
		  //时间格式化
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
              httpurl+=this.url.add;
              method = 'post';
            let formData = Object.assign(this.model, values);
            //时间格式化

            console.log(formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })



          }
        })
      },
      handleCancel () {
        this.close()
      },


    }
  }
</script>

<style lang="less" scoped>

</style>